package com.squirrel.common.component;

import com.squirrel.common.exception.BaseException;
import com.squirrel.common.response.code.CodeEnum;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.Path;

/**
 * @Description NonStaticResourceHttpRequestHandler
 * @Author 关远键
 * @Version v1.0.0
 * @Date 2021/11/16 10:03
 */
@Deprecated
@Component
public class NonStaticResourceHttpRequestHandler extends ResourceHttpRequestHandler {

    public final static String ATTR_FILE = "NON-STATIC-FILE";

    @Override
    protected Resource getResource(HttpServletRequest request) {
        try {
        final Path filePath = (Path) request.getAttribute(ATTR_FILE);
        return new FileSystemResource(filePath);

        } catch (Exception e) {
            throw new BaseException(CodeEnum.NON_RESOURCE);
        }
    }

}

